package com.xiaoq.matrix.minipro.endpoint;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.io.IOUtils;
import org.springframework.core.io.ClassPathResource;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.xiaoq.coms.util.ClassPathResourceUtil;


@RestController
@RequestMapping("/wxa")
public class WechatMockServerEndpointB {
	
	private void responseFromFile(HttpServletResponse resp, String path) {
		InputStream ins = null;
		try {
			ins = ClassPathResourceUtil.getInputStream(path);
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		try {
			resp.setCharacterEncoding("UTF-8");
			List<String> lines = IOUtils.readLines(ins, "UTF-8");
			for (String line : lines) {
				resp.getWriter().println(line);
				System.out.println(line);
			}
			resp.getWriter().flush();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		IOUtils.closeQuietly(ins);
	}
	
	@PostMapping(value = "/modify_domain", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	public void modifyDomain(HttpServletResponse resp) {
		responseFromFile(resp, "/weapp/mock/modify_domain.json");
	}
	
	@PostMapping(value = "/commit", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	public void commitCode(HttpServletResponse resp) {
		responseFromFile(resp, "/weapp/mock/commit_code.json");
	}
	
	@GetMapping(value = "/get_qrcode")
	public void getQrCode(HttpServletResponse resp) {
		/*
		 	HTTP/1.1 200 OK
			Connection: close
			Content-Type: image/jpeg 
			Content-disposition: attachment; filename="QRCode.jpg"
			Date: Sun, 06 Jan 2013 10:20:18 GMT
			Cache-Control: no-cache, must-revalidate
			Content-Length: 339721
		 */
		resp.setContentType(MediaType.IMAGE_JPEG_VALUE);
		resp.setHeader("Content-Disposition", "attachment; filename=\"QRCode.jpg\"");
		resp.setHeader("Connection", "close");
		resp.setHeader("Cache-Control", "no-cache, must-revalidate");
		
		InputStream ins = null;
		try {
			File file = new ClassPathResource("/weapp/mock/qrCode.jpg").getFile();
			ins = new FileInputStream(file);;
			resp.setContentLength((int) file.length());
			IOUtils.copy(ins, resp.getOutputStream());
		} catch (IOException e) {
			e.printStackTrace();
		}
		IOUtils.closeQuietly(ins);
	}
	
	@GetMapping(value = "/get_category", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	public void getCategory(HttpServletResponse resp) {
		responseFromFile(resp, "/weapp/mock/category.json");
	}
	
	@GetMapping(value = "/get_page", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	public void getPage(HttpServletResponse resp) {
		responseFromFile(resp, "/weapp/mock/page.json");
	}
	
	@PostMapping(value = "/submit_audit", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	public void submitAudit(HttpServletResponse resp) {
		responseFromFile(resp, "/weapp/mock/submit_audit.json");
	}
	
	@PostMapping(value = "/get_auditstatus", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	public void getAuditStatus(HttpServletResponse resp) {
		responseFromFile(resp, "/weapp/mock/audit_status.json");
	}
	
	@GetMapping(value = "/get_last_auditstatus", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	public void getLastAuditStatus(HttpServletResponse resp) {
		responseFromFile(resp, "/weapp/mock/last_audit_status.json");
	}
	
	@PostMapping(value = "/release", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	public void release(HttpServletResponse resp) {
		responseFromFile(resp, "/weapp/mock/release.json");
	}
	
}
